import { userMessageService } from '@/api/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'

//用户模块 token setToken removeToken
export const useUserStore = defineStore(
  'evet_user',
  () => {
    //数据
    const token = ref('')
    //方法
    const setToken = (newToken) => {
      //设置token
      token.value = newToken
    }
    const removeToken = () => {
      //移除token
      token.value = ''
    }

    // 获取用户信息并保存
    const userInfo = ref({})

    const setUserInfo = async () => {
      const res = await userMessageService()
      // console.log(res)
      userInfo.value = res.data.data
    }

    // 移除用户信息
    const removeUserInfo = () => {
      userInfo.value = {}
    }

    return {
      token,
      setToken,
      removeToken,
      userInfo,
      setUserInfo,
      removeUserInfo
    }
  },
  {
    persist: true
  }
)
